using System;
using Vici.Mvc;

namespace Demo.ViciMvc
{
    [Url("employee/edit/{employee}")]
    [Url("employee/add","Add")]
    public class editemployee : RestrictedController
    {
        [BeforeAction]
        public void AddJavascript()
        {
            View.RegisterJavascriptInclude("FormTools", true, "~/Content/JavaScript/formTools.js");
        }

        public void Run(Employee employee)
        {
            if (employee == null)
                employee = new Employee();

            EmployeeForm form = new EmployeeForm(employee);
            
            form.Bind();

            if (form.Validated)
            {
                DataService.Save(employee);

                Response.Redirect("~/main");
            }
        }

        public void Add()
        {
            Run(null);
        }

        [Ajax("Ajax.UpdateDepartments", UseFormData = true)]
        public static string UpdateDepartmentDropDown()
        {
            Branch branch = DataService.LoadBranch(FormData.Get<int>("Branch"));

            DropdownControl control = new DropdownControl("Department");

            control.Id = "DepartmentControl";
            control.KeyMember = "DepartmentID";
            control.ValueMember = "Name";
            control.ShowBlank = true;
            control.BlankKey = 0;
            control.BlankValue = "-Select-";

            control.DataSource = DataService.GetAllDepartmentsForBranch(branch);

            return control.Render(new View()); // just specify an empty view object
        }
    }
}
